package com.tang.player.logic;

import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.view.SurfaceHolder;

import com.tang.player.beans.MediaBean;
import com.tang.player.listeners.IPlayCallback;

/**
 * @author txf
 * @Title
 * @package com.tang.player.logic
 * @date 2017/3/17 0017
 */

public class MediaPlayerProvider extends AbsMediaPlayerProvider {
    private final String TAG = getClass().getName();
    private static MediaPlayerProvider mMediaPlayerProvider;
    
    private TMediaPlayer mTMediaPlayer;

    private MediaPlayerProvider() {
    }
    public static MediaPlayerProvider getInstance() {
        if (mMediaPlayerProvider == null)
            mMediaPlayerProvider = new MediaPlayerProvider();
        return mMediaPlayerProvider;
    }

    public TMediaPlayer getMediaPlayer() {
        return mTMediaPlayer;
    }

    public void release() {
        if (mTMediaPlayer != null) {
            mTMediaPlayer.setAllPlayCallback(null);
            mTMediaPlayer.release();
            mTMediaPlayer = null;
        }
    }
    public void start(Context context,IPlayCallback l, SurfaceHolder holder, MediaBean bean) {
        if (mTMediaPlayer != null) {
            release();
        }
        if (holder == null || bean == null || bean.getUrl() == null || bean.getUrl().length() < 0)
            return;
        mTMediaPlayer = new TMediaPlayer();
        mTMediaPlayer.setDisplay(holder);
        mTMediaPlayer.setAllPlayCallback(l);
        mTMediaPlayer.setDataSource(context, Uri.parse(bean.getUrl()));
        mTMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mTMediaPlayer.prepareAsync();
    }
}
